<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    width="850px"
  >
    <div class="form-box">
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="名称" prop="name" v-if="!(form.decorationTypeArea==4)">
          <el-input :disabled="form.decorationTypeArea==1" v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="选择游戏" prop="context" v-if="form.decorationTypeArea==4">
          <el-select v-model="form.context" placeholder="请选择游戏" clearable @change="selectGameType">
              <el-option v-for="dict in allGameList" :key="dict.id" :label="dict.name" :value="dict.id" />
            </el-select>
        </el-form-item>
        <el-form-item label="跳转链接" prop="pcJump" >
          <el-input v-model="form.pcJump" placeholder="请输入PC端跳转跳转链接" />
        </el-form-item>
        <el-form-item label="排序" prop="sort" v-if="!(form.decorationTypeArea==1||form.decorationTypeArea==3)">
          <el-input v-model="form.sort" placeholder="请输入排序" type="number"/>
        </el-form-item>
        <!-- 游戏icon区不能自定义图片 -->
        <el-form-item label="图片" prop="img" v-if="form.decorationTypeArea!=4" :class="{'hiddenupload':imgShowList.length}">
          <el-upload
                  :action="uploadImgUrl"
                  :file-list="imgShowList"
                  :show-file-list="true"
                  list-type="picture-card"
                  :limit="1"
                  :on-success="handleUploadSuccess"
                  :on-remove="handleRemove"
                  :on-preview="handlePictureCardPreview"
                  :headers="headers"
                >
                  <i class="el-icon-plus"></i>
                </el-upload>
        </el-form-item>
        <el-form-item label="背景色" prop="context" v-if="form.decorationTypeArea==1">
          <el-color-picker v-model="form.context"></el-color-picker>
        </el-form-item>
        <el-form-item label="启用状态">
          <el-radio-group v-model="form.state">
            <el-radio  :label="1">启用</el-radio>
            <el-radio  :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 富文本 -->
        <div class="tip" v-if="form.decorationTypeArea==2">
          提示：以下文本框内容，在点击图片的时候展示。
        </div>
        <div v-if="form.decorationTypeArea==2" style="border: 1px solid #ccc; margin-top:10px">
          <myEditor v-model="form.context" @input="editorInput"></myEditor>
        </div>

        <div class="center">
          <el-button @click="close()">取消</el-button>
          <el-button type="primary" @click="submitForm('form')" v-loading="loading">提交</el-button>
        </div>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
import { getUserProfile } from "@/api/system/user";
import myEditor from "@/components/WangEditor/index.vue"
import { getToken } from "@/utils/auth";
import shopPageSettingApi from '@/api/tenantApis/shopPageSetting'
export default {
  name: "",
  components: {myEditor},
  data() {
    return {
      dialogVisible: false,
      loading: false,
      title: "新增",
      isEdit: false,
      imgShowList: [],
      // 表单参数
      form: {
        name:'',
        type:'',
        img:'',
        mobileJump:'',
        pcJump:'',
        context:'',
        state:1,
      },
      headers:{},
      rules: {
        img:[
          { required: true, message: "请上传图片", trigger: "change" }
        ],
      }
    };
  },
  computed:{
    allGameList(){
      return this.$store.getters.allGameList
    }
  },
  created() {
    this.headers = {
      Authorization: "Bearer " + getToken(),
    };
    this.uploadImgUrl = process.env.VUE_APP_BASE_API + "/upload/image";
    
  },
  methods: {
    editorInput(e){
      this.form.context = e
    },
    getUser() {
      getUserProfile().then(response => {
        this.user = response.data
      });
    },
    selectGameType(e){
      if(e){
        this.allGameList.forEach(element => {
          if(element.id==e){
            this.form.img = element.icon
            this.form.name = element.name
            this.form.mobileJump = element.type
          }
        });
      }
    },
    // 打开弹窗
    open(flag, data,type) {
      this.imgShowList = [];
      this.form={
        name:'',
        img:'',
        mobileJump:'',
        pcJump:'',
        context:'',
        state:1,
      }
      if (flag == "edit") {
        this.title = "修改";
        this.isEdit = true;
        this.form = JSON.parse(JSON.stringify(data))
        this.imgShowList = [{ url: data.img, name: "" }]
        if(this.form.decorationTypeArea==4){
          this.form.context = Number(this.form.context)
        }
      } else {
        this.form.decorationTypeArea = data.area
        this.form.decorationTypeType = data.type
        this.isEdit = false;
      }
      this.dialogVisible = true;
      // this.$nextTick(() => {
      //   this.reset();
      // });
    },
    // 重置
    reset() {
      this.$refs.form.resetFields();
    },
    submitForm(formName) {
      this.$refs["" + formName].validate(valid => {
        if (valid) {
          if (this.form.id) {
            // 编辑
            shopPageSettingApi.decorationInfoEdit(this.form).then(res => {
              this.$message.success(res.msg);
              this.dialogVisible = false;
              this.$parent.getAllConfig();
            });
          } else {
            // 新增
            shopPageSettingApi.decorationInfoAdd(this.form).then(res => {
              this.$message.success(res.msg);
              this.dialogVisible = false;
              this.$parent.getAllConfig();
            });
          }
        } else {
          this.$alert("请填写完整！");
        }
      });
    },
    // 图片上传成功
    handleUploadSuccess(res, file, fileList) {
      if (res.code == 200) {
        // 取得新上传的文件
        this.imgShowList = [{ url: res.data.filePath, name: "" }];
          this.form.img  = res.data.filePath
      }
    },
    // 详情图片删除
    handleRemove(response, fileList, file) {
      this.imgShowList = []
      this.form.img = ''
    },
    // 图片预览
    handlePictureCardPreview(file) {
      this.$viewerApi({
        images: [file.url]
      });
    },
    uploadNumber(file, fileList) {
      //限制图片数量
      this.$modal.msgError("最多只能上传1张图片");
    },
    close() {
      this.dialogVisible = false;
    }
  }
};
</script>
<style scoped lang="css">
.tip{
  color: red;
}
.form-box {
  padding-top: 20px;
}

.form-box >>> .el-form-item__content {
  max-width: 655px;
}

</style>
